package com.dashan.looperpager.adapters;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;
import java.util.List;

public class LooperPagerAdapter extends PagerAdapter {


    private List<Integer> mPics = null;

    public LooperPagerAdapter(List<Integer> pics) {
        this.mPics = pics;
    }

    @Override
    public int getCount() {
        return mPics == null ? 0 : Integer.MAX_VALUE;//用作伪实现无限循环
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        /**
         * ==========>>这里获取View  获取Object 做判断
         */
        return view == object;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        int realPosition = position % mPics.size();
        ImageView imageView = new ImageView(container.getContext());
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setImageResource(mPics.get(realPosition));
        //设置完数据以后，就添加到容器里面
        /**
         * 个人理解
         * ==========>>这里添加View  返回Object
         */
        container.addView(imageView);//添加View
        return imageView;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);//销毁View
    }

    /**
     * 返回mPics真正的大小
     */
    public int getDataRealSize() {
        return mPics == null ? 0 : mPics.size();
    }

}
